<?php
session_start();
isLogin();

if(isset($_POST["Submits"])){
	$des=$_POST['description']?$_POST['description']:mysubstr(preg_replace("/\s/",'',strip_tags($_POST['content'])),180,false);
	$arr=array(
		'title' => $_POST['title'],		   
		'catalog' => $_POST['catalog'],		   
		'author' => $_POST['author'],		   
		'a_from' => $_POST['a_from'],		   
		'keywords' => $_POST['keywords'],		   
		'description' => $des,		   
		'content' => $_POST['content'],		
		'top' => $_POST['top'],		
		'label' => $_POST['label'],		
		'rec' => $_POST['rec'],		
		'pic' => $_POST['pic'],	
		'filename' => $_POST['filename'],
		'toutiao' => $_POST['toutiao']
	);
	checkNotNull(array('title','content'),"?".$_SERVER['QUERY_STRING'],"文章");
	if($_REQUEST['act']=='modify'){
		$sql=getUpdateSql($arr,'article','where id='.$_GET['id'],2);
		$pre="修改";
	}else{
		$a=array('dt' => time());
		$arr=array_merge($arr,$a);
		$sql=getInsertSql($arr,'article',2);
		$pre="添加";
	}
	if(mysql_query($sql)){
		reload_js($pre."成功","index.php?cmd=article");
	}else{
	//	halt_js(mysql_error());
		reload_js($pre."失败，原因：程序错误，请联系技术人员进行修改","index.php");
	}
}

$arr="";
$_SERVER['HTTP_REFERER']=$_SERVER['HTTP_REFERER']?$_SERVER['HTTP_REFERER']:$_GET['from'];
if($_REQUEST['act']=='modify'){
	$arr=get_list_by_id('article','id',$_REQUEST['id']);
	if(!$arr){
		reload_js("错误的参数p_id","?cmd=article");
	}
	$thiscat=$arr['catalog'];
}else if($_REQUEST['act']=='del'){
	$id=$_GET['id']?$_GET['id']:$_REQUEST['ids'];
	$sql="delete from ".$prefix."article where id in(".$id.")";
	$pre="删除";
	if(mysql_query($sql)){
		reload_js($pre."成功",$_SERVER['HTTP_REFERER']);
	}else{
		reload_js($pre."失败，原因：程序错误，请联系技术人员进行修改","index.php");
	}
}else if($_REQUEST['act']=='move'){
	$sql="update ".$prefix."article set catalog=".$_POST['moveto']." where id in (".$_POST['ids'].")";
	$pre="移动";
	if(mysql_query($sql)){
		reload_js($pre."成功",$_SERVER['HTTP_REFERER']);
	}else{
		reload_js($pre."失败，原因：程序错误，请联系技术人员进行修改","index.php");
	}
}
function get_catalog_list_by_id($id,$ji){
	global $prefix,$thiscat;
	
	$result=mysql_query("select * from ".$prefix."column where pid=".$id." order by paixu ");
	$pre='';
	for($i=0;$i<$ji;$i++){$pre .= "--";}
	while($arr=mysql_fetch_array($result)){
		$c='';
		if($_GET['catalog']==$arr['id']||$thiscat==$arr['id']){$c='selected';}
		$d='';
		if($arr['type']!='article'){$d='disabled="disabled"';}
		echo "<option value='".$arr['id']."' ".$c.$d.">".$pre.$arr['name']."</option>";
		$a=mysql_query("select * from ".$prefix."column where pid=".$arr['id']." order by paixu ");	
		if($b=mysql_fetch_array($a)){
			$ji++;
			get_catalog_list_by_id($arr['id'],$ji);
			$ji=0;
		}
	}
}

$page=$_REQUEST['page']?($_REQUEST['page']==0?1:$_REQUEST['page']):1;
?>
<script type="text/javascript" src="../model/fckeditor/fckeditor.js"></script>
<script language="javascript" src="../templates/<?php echo $moban;?>/js/common.js"></script>
<script language="javascript">
changeWebTitle('文章管理');

function showit(obj){
	var m=document.getElementById('moveto');
	if(obj.value=='pmove'){m.style.display='';}else{m.style.display='none';}
}

function checkAll(){
	var objs=document.getElementsByName("checkbox");
	for(var i=0;i<objs.length;i++){
		if(objs[i].checked==false){objs[i].checked=true;}else{objs[i].checked=false;}
	}
}

function checkSubmit(){
	var ids='';
	var objs=document.getElementsByName("checkbox");
	for(var i=0;i<objs.length;i++){
		if(objs[i].checked==true){
			ids+=','+objs[i].value;
		}
	}
	if(ids==''){alert('没有选择任何记录');return false;}
	ids=ids.substring(1);
	var o=document.getElementById("caozuo");
	if(o.value=='pmove'){
		var t=document.getElementById("moveto");
		for(i=0;i<t.length;i++){
			if(t[i].selected==true){
				var move = t[i].innerHTML;  
			}
		}
		if(confirm("您准备将ID号为"+ids+"的记录移动到 "+move+" ?")){
			document.getElementById("ids").value=ids;
			document.getElementById("act").value='move';
			return true;
		}
	}else if(o.value=='pdel'){
		if(confirm("您确定将ID号为"+ids+"的记录删除？删除后将不可恢复!")){
			document.getElementById("ids").value=ids;
			document.getElementById("act").value='del';
			return true;
		}
	}
}
</script>
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>


<div class="ContentRight">
    <div class="ContentRightTop">
      <div class="BarLeft"></div>
      <div class="ContentRightTopText">文章管理
	  <?php 
	  if($_GET['act']==''){
	  	echo '<a href="?cmd='.$_REQUEST['cmd'].'&amp;act=add">（添加）</a>';
	  }else{
	  	echo "<a href='".$_SERVER['HTTP_REFERER']."'>（返回）</a>";
	  }
	  ?>
	  </div>
      <div class="BarRight"></div>
      <div class="Cal"></div>
    </div>
  <div class="ContentRightBox">
	<?php if($_REQUEST['act']==''){?>
<form action="" name="listform" method="post" onsubmit="return checkSubmit();">
<table width="100%" cellpadding="0" class="Table1" bordercolor="#4ab902" border="1" cellspacing="0">
        <tr>
          <td height="25" colspan="5" align="center">
          注意：生成html后，前台才能正常访问&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;筛选：
          <a href="?cmd=article">全部</a>&nbsp;&nbsp;
          <a href="?cmd=article&page=<?php echo $page;?>&top=1&rec=<?php echo $_GET['rec']?>&catalog=<?php echo $_GET['catalog']?>">置顶文章</a>&nbsp;&nbsp;
          <a href="?cmd=article&page=<?php echo $page;?>&top=<?php echo $_GET['top']?>&rec=1&catalog=<?php echo $_GET['catalog']?>">推荐文章</a>&nbsp;&nbsp;
          &nbsp;&nbsp;
          <select name="catalog" onChange="location.href='?cmd=article&page=<?php echo $page;?>&top=<?php echo $_GET['top']?>&rec=<?php echo $_GET['top']?>&catalog='+this.value;">
          <?php
		  get_catalog_list_by_id(0,0);
		  ?>
          </select>
          </td>
        </tr>
        <tr>
          <td width="5%" align="center">
          <input type="hidden" name="ids" id="ids" value="" />
          <input type="hidden" name="act" id="act" value="" />
          <input type="checkbox" name="all" id="all" onclick="checkAll();">
          </td>
          <td width="9%" height="25" align="center">ID</td>
          <td width="55%" align="center">标题</td>
          <td width="14%" align="center">类别</td>
          <td width="17%" align="center">操作</td>
        </tr>
		<?php 
		$pagesize=10;
		$sqladd='';
		if($_GET['catalog']!=''){
			$sqladd="where catalog='".$_GET['catalog']."' ";
		}
		if($_GET['top']!=''){
			if(!$sqladd){
				$sqladd="where top=1 ";
			}else{
				$sqladd.=" and top=1";
			}
		}
		if($_GET['rec']!=''){
			if(!$sqladd){
				$sqladd="where rec=1 ";
			}else{
				$sqladd.=" and rec=1";
			}
		}
		if($_GET['key']==''){
			if(!$sqladd){
				$sqladd="where title like'%".$_GET['key']."%' ";
			}else{
				$sqladd.=" and title like'%".$_GET['key']."%' ";
			}
		}
		$sql="select * from ".$prefix."article ".$sqladd." order by id desc limit ".($page-1)*$pagesize.",".$pagesize;
		//halt_js($sql);
		$result=mysql_query($sql);
		while($arr=mysql_fetch_array($result)){
		?>
		<tr>
		  <td width="5%" align="center">
          <input type="checkbox" name="checkbox" value="<?php echo $arr['id']?>">
          </td>
          <td width="9%" height="25" align="center"><?php echo $arr['id']?></td>
          <td width="55%" align="center">
		  	<a href="<?php echo "../showarticle.php?id=".$arr['id']?>" target="_blank"><?php echo mysubstr($arr['title'],60,'..');if($arr['top']){echo "[顶]";} if($arr['rec']){echo "[荐]";}if($arr['toutiao']){echo "[头条]";}?></a>		  </td>
          <td width="14%" align="center"><?php $cat=get_field_by_id('column','name','id',$arr['catalog']);echo $cat['name'];?></td>
          <td width="17%" align="center"><a href="<?php echo "?cmd=".$_REQUEST['cmd']."&id=".$arr['id']."&act=modify"?>">修改</a>&nbsp;<a href="<?php echo "?cmd=".$_REQUEST['cmd']."&id=".$arr['id']."&act=del"?>" onclick="return confirm('您确认删除该条记录？删除后将不可恢复！');">删除</a></td>
        </tr>
		<?php
		}
		?>
        <tr>
          <td height="25" colspan="5" align="center">
		  <?php 
		  pagefy(get_list_count('article',$sqladd),$pagesize,'');
		  echo $pagenav;
		  ?>		  </td>
        </tr>
        <tr>
          <td height="25" colspan="5" align="left">
          <select name="caozuo" id="caozuo" onChange="showit(this);">
            <option value="pdel">批量删除</option>
            <option value="pmove">批量移动到</option>
          </select>
          <select name="moveto" id="moveto" style="display:none">
          <?php
		  get_catalog_list_by_id(0,0);
		  ?>
          </select>
          &nbsp;&nbsp;<input type="submit" name="zhixing" id="zhixing" value="执行"></td>
        </tr>
      </table>
   </form>   
    <?php }else{?>
	  <form action="" name="productform" method="post">
    <table width="100%" cellpadding="0" class="Table1" bordercolor="#4ab902" border="1" cellspacing="0">
        <tr>
          <td width="22%" height="25" align="right">文章标题：</td>
          <td width="78%">
            <input name="title" type="text" id="title" value="<?php echo $arr['title'];?>" size="50"/>
            <span class="STYLE1">*</span>&nbsp;</td>
        </tr>
        <tr>
          <td height="41" align="right">所属类别：</td>
          <td>
          <select name="catalog">
          <?php
		  get_catalog_list_by_id(0,0);
		  ?>
          </select>
          </td>
        </tr>
        <tr>
          <td height="25" align="right">作者：</td>
          <td>
          <input name="author" type="text" id="author" size="25" value="<?php echo $arr['author'];?>"/></td>
        </tr>
        <tr>
          <td height="25" align="right">来源：</td>
          <td><input name="a_from" type="text" id="a_from" size="25" value="<?php echo $arr['a_from'];?>"/></td>
        </tr>
        <tr>
          <td height="25" align="right">标签：</td>
          <td><input name="label" type="text" id="label" size="50" value="<?php echo $arr['label'];?>"/> 
          多个用英文逗号分隔</td>
        </tr>
        <tr>
          <td height="25" align="right">标题图片：</td>
          <td><input name="pic" type="text" id="pic" value="<?php echo $arr['pic'];?>" size="50"/> 
          110*150</td>
        </tr>
        <tr>
          <td height="25" align="right">关键字：</td>
          <td><input name="keywords" type="text" id="keywords" size="50" value="<?php echo $arr['keywords'];?>"/>
          多个用英文逗号分隔</td>
        </tr>
        <tr>
          <td height="25" align="right">导语：</td>
          <td><textarea name="description" id="description" cols="45" rows="5"><?php echo $arr['description'];?></textarea></td>
        </tr>
        <tr>
          <td height="25" align="right">内容：</td>
          <td><script type="text/javascript">
			var sBasePath = '../model/fckeditor/' ;
			 
			var oFCKeditor = new FCKeditor( 'content' ) ;
			oFCKeditor.BasePath	= sBasePath ;
			oFCKeditor.Height	= 350 ;
			oFCKeditor.Width	= 580 ;
			oFCKeditor.ToolbarSet="Fengsh";
			oFCKeditor.Value	= "<?php echo str_replace('"','\"',stripslashes(preg_replace('/\r\n/','',$arr['content'])));?>";
			oFCKeditor.Create() ;
		</script>		  
          </td>
        </tr>
        <tr>
          <td height="25" align="right">指定文件名：</td>
          <td><input name="filename" type="text" id="filename" size="25" value="<?php echo $arr['filename'];?>"/>
            例如：myarticle.html 必须带后缀名，并不能有重名</td>
        </tr>
        <tr>
          <td height="25" align="right">其他：</td>
          <td>置顶：<input type="radio" name="top" value="1" id="top_0" checked>是
                <input name="top" type="radio" id="top_1" value="0" <?php if(!$arr['top']){echo "checked";}?>>否  
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              推荐：<input type="radio" name="rec" value="1" id="rec_0" checked>是
            <input name="rec" type="radio" id="rec_1" value="0"  <?php if(!$arr['rec']){echo "checked";}?>>
            否
                
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 头条：
              <input type="radio" name="toutiao" value="1" id="rec_2" checked="checked" />
              是
              <input name="toutiao" type="radio" id="rec_3" value="0"  <?php if(!$arr['toutiao']){echo "checked";}?> />
否
                
              &nbsp;</td>
        </tr>
        <tr>
          <td height="25" align="right">&nbsp;</td>
          <td><input name="Submits" type="submit" id="Submits" value=" 提  交" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input name="Reset" type="reset" id="Reset" value=" 重 置 " />          </td>
        </tr>
      </table>
    </form>
    <?php }?>
  </div>
    <div class="Cal"></div>
</div>